function packages(){}
packages.prototype = {
    
    statusAvailable : 1,
    statusBooked : 2,
    statusUnavailable : 3,
    statusError : 4,
    inRequest : false,
    
    bookTicket : function(seatId, packageId){
        this.sendRequest('/package/book-ticket', seatId, packageId);
    },
    
    unbookTicket : function(seatId, packageId){
        this.sendRequest('/package/unbook-ticket', seatId, packageId);
    },
    
    multiSectorSelectTicket : function(packageId){
        var thisObject = this;
        var sectors = {
            package:packageId,
            data: []
        };
        var sectorsSelected = false;
        
        $(".multiSectorSelectTicket").each(function() {
            var qnt = $(this).val();
            var sectorId = $(this).attr("id");
            if(qnt > 0){
                sectors.data.push({sector: sectorId, qnt: qnt});
                sectorsSelected = true;
            }
        });
        
        if(sectorsSelected) {
            ajaxLoader.ajaxStart();
            $.ajax({
                url: '/package/book-multiple-tickets-sectors',
                data: {sectors: sectors},
                method: 'post',
                dataType: 'json',
                success: function(json) {
                    thisObject.validateMultiBookingResponse(json);
                },
            });
        }else{
            var modal = new Modal({
                title: translate["tickets-js-modal-title-alert"],
                body : '
' + translate["tickets-js-error-no-ticket-selected"] + '
',
                buttonOK : false,
                cancel : function(){
                    modal.hide();
                }
            });
            modal.show();
        }
    },
    
    sendRequest : function(url, seatId, packageId){
        $('#'+seatId).css('visibility','hidden');
        $('#'+seatId).after('');
        
        this.sendAjax(url, seatId, packageId);
    },
    
    sendAjax : function(url, seatId, packageId){
        var thisObject = this;
        
        if(this.inRequest){
            var sendRequestFunction = thisObject.sendAjax.bind(this);
            setTimeout( function(){
                sendRequestFunction(url, seatId)
            }, 100 );
            return false;
        }
        
        this.inRequest = true;
        $.ajax({
            url: url,
            data: {seat: seatId, package: packageId},
            method: 'post',
            dataType: 'json',
            success: function(json) {
                thisObject.inRequest = false;
                thisObject.validateBookingResponse(json, seatId);
            },
        });
    },
    
    validateBookingResponse : function(json, ticketId){
        $('#'+ticketId).css('visibility','visible');
        $('#'+ticketId).next('.spin').remove();
        switch (json.status) {
            case 0:
                window.location.reload();
                break;
                
            case this.statusAvailable:
                $('#'+ticketId).attr("class", "seat disponivel");
                $('#'+ticketId).attr("onClick", "packages.bookTicket("+ticketId+")");
                break;
                
            case this.statusBooked:
                $('#'+ticketId).attr("class", "seat reserva");
                $('#'+ticketId).attr("onClick", "packages.unbookTicket("+ticketId+")");
                break;
                
            case this.statusUnavailable:
                $('#'+ticketId).attr("class", "seat reservada");
                $('#'+ticketId).removeAttr("onClick");
                
                var modal = new Modal({
                    title: translate["tickets-js-modal-title-alert"],
                    body : ''+json.message+'
',
                    buttonOK : false,
                });
                
                modal.show();
                
                break;
                
            case this.statusError:
                if(typeof json.url == 'undefined'){
                    var modal = new Modal({
                        title: translate["tickets-js-modal-title-alert"],
                        body : ''+json.message+'
',
                        buttonOK : false,
                    });
                }else{
                    var modal = new Modal({
                        title: translate["tickets-js-modal-title-alert"],
                        body : ''+json.message+'
',
                        buttonOK : false,
                        cancel : function(){
                            window.location = json.url;
                        }
                    });
                }
                
                modal.show();
                break;
            default:
                var modal = new Modal({
                    title: translate["tickets-js-modal-title-alert"],
                    body : '' + translate["tickets-js-generic-error"] + '
',
                    buttonOK : false,
                    cancel : function(){
                        window.location.reload();
                    }
                });
            
                modal.show();
                break;
        }
        
        if(json.cart_total != undefined){
            $('#cart_total').text(json.cart_total);
        }
    },
    
    validateMultiBookingResponse : function(json){
        if(json.cart_total != undefined){
            $('#cart_total').text(json.cart_total);
        }
        
        switch (json.status) {
            case this.statusBooked:
                window.location.href = '/shopping-cart';
                break;
                
            case this.statusError:
                ajaxLoader.ajaxStop();
                if(typeof json.url == 'undefined'){
                    var modal = new Modal({
                        title: translate["tickets-js-modal-title-alert"],
                        body : ''+json.message+'
',
                        buttonOK : false,
                    });
                }else{
                    var modal = new Modal({
                        title: translate["tickets-js-modal-title-alert"],
                        body : ''+json.message+'
',
                        buttonOK : false,
                        cancel : function(){
                            window.location = json.url;
                        }
                    });
                }
                
                modal.show();
                break;
            default:
                ajaxLoader.ajaxStop();
                var modal = new Modal({
                    title: translate["tickets-js-modal-title-alert"],
                    body : '' + translate["tickets-js-generic-error"] + '
',
                    buttonOK : false,
                    cancel : function(){
                        window.location.reload();
                    }
                });
            
                modal.show();
                break;
        }
        
    },
    
    saleSectorExpress : function(sectorId, ticketRows, packageId){
    	
        var thisObject = this;
    	var comboRowsTickets = '';
		comboRowsTickets += '';
		comboRowsTickets += '
 ';
		comboRowsTickets += 'Obs.: ' + translate["tickets-js-tip-message"] + '
';
    	
        var modal = new Modal({
            title: translate["tickets-js-tickets-quantity-message"],
            body : comboRowsTickets,
            buttonOK : true,
            cancel : function(){
            	modal.hide();
            },
            confirm : function(){
            	var ticketQuantity = $('#ticket_quantity').val();
                var sectorId = $('#sectorId').val();
                ajaxLoader.ajaxStart();
                $.ajax({
                    url: '/package/book-multiple-tickets',
                    data: {sectorId: sectorId, ticketQuantity: ticketQuantity, packageId: packageId},
                    method: 'post',
                    dataType: 'json',
                    success: function(json) {
                        thisObject.validateMultiBookingResponse(json);
                    }
                });
            	
            }
        });
    
        modal.show();
    },
    
    showMessage: function(message, url){
        if(typeof url == 'undefined' || url == false){
            var modal = new Modal({
                title: translate["tickets-js-modal-title-alert"],
                body : ''+message+'
',
                buttonOK : false,
            });
        }else{
            var modal = new Modal({
                title: translate["tickets-js-modal-title-alert"],
                body : ''+message+'
',
                buttonOK : false,
                cancel : function(){
                    window.location = url;
                }
            });
        }
        
        modal.show();
    }
}
var packages = new packages();